package com.example.appinting;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.appinting.SQL.MysqlHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.example.appinting.R;

public class QueueActivity extends Activity{

    private TextView queuePosition;
    private String doctor_id="",patient_id="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((R.layout.activity_queue));
        System.out.println("进入queueActivity");
        patient_id = this.getIntent().getStringExtra("patient_id");
        doctor_id = this.getIntent().getStringExtra("doctor_id");



        queuePosition = (TextView) findViewById(R.id.position);
        int queue_position = getPatientPosition(patient_id,doctor_id);
        int finish_num = getFinishNum(doctor_id);
        queuePosition.setText(Integer.toString(queue_position-finish_num));
        System.out.println("QueueActivity执行完成");

    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }

    int getPatientPosition (String patient_id,String doctor_id){
        Connection connection = null;
        PreparedStatement statement = null;

        ResultSet resultSet = null;

        try {
            System.out.println("getPatientPosition");
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "SELECT position FROM queue WHERE doctor_id = " + doctor_id + " and patient_id = "+patient_id+" ORDER BY id DESC LIMIT 1";
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            resultSet = statement.executeQuery();
            while (resultSet.next() ) {
                int position = resultSet.getInt(1);
                System.out.println(patient_id+"当前position为"+position);
                // 处理查询结果
                return position;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return 0;
    }

    int getFinishNum (String doctor_id){
        Connection connection = null;
        PreparedStatement statement = null;

        ResultSet resultSet = null;

        try {
            System.out.println("getFinishNum");
            // 连接到 MySQL 数据库
            connection = MysqlHelper.getConnection();
            // 准备 SQL 语句
            String sql = "SELECT finish_number FROM finishnumber WHERE doctor_id = " + doctor_id ;
            statement = connection.prepareStatement(sql);

            // 执行 SQL 语句
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                int finishnumber = resultSet.getInt(1);
                System.out.println("医生"+doctor_id+"当前finishinumber为"+finishnumber);
                // 处理查询结果
                return finishnumber;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接和资finishnumber源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return 0;
    }
}
